package ngames.com.sushitv.base;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;

import java.util.ArrayList;

import ngames.com.sushitv.interfaces.OnItemClickListener;

/**
 * Created by Administrator on 2016/3/3.
 */
public abstract class BaseListAdapter<T> extends RecyclerView.Adapter {
    protected Context mContext;
    protected LayoutInflater mInflater;
    protected ArrayList<T> mDataList;
    protected OnItemClickListener mOnItemClickListener;

    public BaseListAdapter(Context context, ArrayList<T> list) {
        mContext = context;
        mInflater = LayoutInflater.from(context);
        mDataList = list;
    }

    @Override
    public int getItemCount() {
        return mDataList == null ? 0 : mDataList.size();
    }

    public T getItem(int position) {
        if (mDataList == null || mDataList.size() <= position) {
            return null;
        }
        return mDataList.get(position);
    }

    public void setItem(T item, int position) {
        if (mDataList == null || mDataList.size() <= position) {
            return;
        }
        mDataList.set(position, item);
        notifyItemChanged(position);
    }

    public void removeItem(int index) {
        if (mDataList == null || mDataList.size() <= index) {
            return;
        }
        mDataList.remove(index);
        notifyDataSetChanged();
    }

    public void setDataList(ArrayList<T> list) {
        if (mDataList != null) {
            mDataList.clear();
        }
        mDataList = list;
        notifyDataSetChanged();
    }

    public void addData(ArrayList<T> list) {
        if (list == null) {
            return;
        }
        if (mDataList == null) {
            mDataList = list;
        } else {
            mDataList.addAll(list);
        }
        notifyDataSetChanged();
    }

    public void clearData() {
        if (mDataList != null) {
            mDataList.clear();
            notifyDataSetChanged();
        }
    }

    public ArrayList<T> getData() {
        return mDataList;
    }

    public void setOnItemClickListener(OnItemClickListener l) {
        mOnItemClickListener = l;
    }

    public OnItemClickListener getOnItemClickListener() {
        return mOnItemClickListener;
    }
}
